/*
    Asignatura: Procesadores de lenguajes.
    Practica de labotario numero 10.
    Autores:
                Guillermo Garcia Juanes (Alu3889)
                Jose Antonio Hernandez Gonzalez (Alu3809)
   
   Fichero: Procesador.java
   Contenido: Procesador
*/
package pl.equipo13.p10;

import java.io.FileInputStream;
import java.io.IOException;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.CommonTree;
import pl.equipo13.p10.tree.*;

public class Procesador {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream(args[0]);
            ANTLRInputStream input = new ANTLRInputStream(fis);
            if ("1".equals(args[1])) {
                Analex analex = new Analex(input);
                /*Token token = analex.nextToken();
                String rs = "";
                while (token.getType() != Token.EOF) {
                    if (token.getChannel() != Token.HIDDEN_CHANNEL) {
                        rs += token.getText() + "\n";
                        System.out.println("Token: " + token.toString());
                    }
                    token = analex.nextToken();
                }     
                System.out.println(rs);*/
                CommonTokenStream tokens = new CommonTokenStream(analex);
                Anasin anasin = new Anasin(tokens); 
                CommonTree arbol = (CommonTree) anasin.instrucciones().getTree(); 
                System.out.println(arbol.toStringTree());
                ASTFrame af = new ASTFrame("Tree", arbol);
                af.setVisible(true);
            } else if ("3".equals(args[1])) {
                AnalexMiArbol analexMA = new AnalexMiArbol(input);
                /*Token token = analexMiArbol.nextToken();
                String rs = "";
                while (token.getType() != Token.EOF) {
                    if (token.getChannel() != Token.HIDDEN_CHANNEL) {
                        rs += token.getText() + "\n";
                        System.out.println("Token: " + token.toString());
                    }
                    token = analexMiArbol.nextToken();
                }     
                System.out.println(rs);*/
                CommonTokenStream tokens = new CommonTokenStream(analexMA);
                AnasinMiArbol anasinMA = new AnasinMiArbol(tokens); 
                anasinMA.setTreeAdaptor(new MiArbolAdaptor(args[0]));
                CommonTree arbol = (CommonTree) anasinMA.instrucciones().getTree(); 
                System.out.println(arbol.toStringTree());
                ASTFrame af = new ASTFrame("Tree", arbol);
                af.setVisible(true);
            } else if ("4".equals(args[1])) {
                AnalexC analexC = new AnalexC(input);
                /*Token token = analexC.nextToken();
                String rs = "";
                while (token.getType() != Token.EOF) {
                    if (token.getChannel() != Token.HIDDEN_CHANNEL) {
                        rs += token.getText() + "\n";
                        System.out.println("Token: " + token.toString());
                    }
                    token = analexC.nextToken();
                }     
                System.out.println(rs);*/
                CommonTokenStream tokens = new CommonTokenStream(analexC);
                AnasinC anasinC = new AnasinC(tokens); 
                CommonTree arbol = (CommonTree) anasinC.entrada().getTree(); 
                //System.out.println(arbol.toStringTree());
                ASTFrame af = new ASTFrame("Tree", arbol);
                af.setVisible(true);
            } else if ("6".equals(args[1])) {
                AnalexXML analexXML = new AnalexXML(input);
                /*Token token = analexXML.nextToken();
                String rs = "";
                while (token.getType() != Token.EOF) {
                    if (token.getChannel() != Token.HIDDEN_CHANNEL) {
                        rs += token.getText() + "\n";
                        System.out.println("Token: " + token.toString());
                    }
                    token = analex.nextToken();
                }     
                System.out.println(rs);*/
                CommonTokenStream tokens = new CommonTokenStream(analexXML);
                AnasinXML anasinXML = new AnasinXML(tokens); 
                CommonTree arbol = (CommonTree) anasinXML.entrada().getTree(); 
                //System.out.println(arbol.toStringTree());
                ASTFrame af = new ASTFrame("Tree", arbol);
                af.setVisible(true);
            }
            // Ventana No se puede
        } catch (RecognitionException ex) {
            System.err.println(ex.getMessage());
        } catch (IOException ex) {
            System.err.println("Problema con el fichero");
        }
    }
}
